package com.jf.blog.service.impl;

import com.google.code.kaptcha.Producer;
import com.jf.blog.service.KaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.image.BufferedImage;
import java.time.Duration;

@Service
@Transactional
public class KaptchaServiceImpl implements KaptchaService {
    @Autowired
    private Producer producer;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private final String kaptcheKey = "kaptcha";

    @Override
    public BufferedImage getKaptcha(String uuid) {
        /*获取图片验证码的文本值*/
        String code = producer.createText();
        /*根据uuid作为key，生成code作为value存入redis并设置2分钟有效*/
        redisTemplate.opsForValue().set(kaptcheKey + "-" + uuid, code, Duration.ofMinutes(2));
        return producer.createImage(code);
    }

    @Override
    public boolean checkKaptcha(String uuid, String code) {
        return false;
    }
}
